package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.model.product.vo.SaveBaseAttrInfoVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.model.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.atguigu.gmall.product.mapper.BaseAttrValueMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lyd
 * @description 针对表【base_attr_value(属性值表)】的数据库操作Service实现
 * @createDate 2025-03-10 20:57:08
 */
@Service
public class BaseAttrValueServiceImpl extends ServiceImpl<BaseAttrValueMapper, BaseAttrValue>
        implements BaseAttrValueService {
    @Autowired
    BaseAttrInfoService baseAttrInfoService;

    @Override
    public List<BaseAttrInfo> attrInfoList(Long c1Id, Long c2Id, Long c3Id) {
        return baseMapper.attrInfoList(c1Id, c2Id, c3Id);
    }

    @Transactional
    @Override
    public void saveAttrInfo(SaveBaseAttrInfoVo vo) {
        if (vo.getId() != null) {
            if (StringUtils.hasText(vo.getAttrName())) {
                //1、1修改base_attr_info平台属性
                baseAttrInfoService.lambdaUpdate()
                        .set(BaseAttrInfo::getAttrName, vo.getAttrName())
                        .set(BaseAttrInfo::getCategoryId, vo.getCategoryId())
                        .eq(BaseAttrInfo::getId, vo.getId())
                        .update();
                //2、修改base_attr_value平台属性值
                //2、1删除前端没有携带的属性值
                List<Long> attrValueIds = vo.getAttrValueList().stream()
                        .filter(item -> item.getId() != null)
                        .map(item -> item.getId())
                        .collect(Collectors.toList());
                if (attrValueIds.size() > 0) {
                    this.lambdaUpdate()
                            .notIn(BaseAttrValue::getId, attrValueIds)
                            .eq(BaseAttrValue::getAttrId, vo.getId())
                            .remove();
                }else {
                    //前端没有传入属性名称,直接删除所有属性值
                    this.lambdaUpdate()
                            .eq(BaseAttrValue::getAttrId, vo.getId())
                            .remove();
                }
                //2、2保存修改的值或者新增的属性值
                List<BaseAttrValue> baseAttrValueList = vo.getAttrValueList().stream()
                        .filter(item -> item.getId() != null)
                        .map(item -> {
                            BaseAttrValue baseAttrValue = new BaseAttrValue();
                            BeanUtils.copyProperties(item, baseAttrValue);
                            return baseAttrValue;
                        }).collect(Collectors.toList());
                this.updateBatchById(baseAttrValueList);
                //保存新增的值
                List<BaseAttrValue> newBaseAttrValues = vo.getAttrValueList().stream()
                        .filter(item -> item.getId() == null)
                        .map(item -> {
                            BaseAttrValue baseAttrValue = new BaseAttrValue();
                            baseAttrValue.setAttrId(vo.getId());
                            baseAttrValue.setValueName(item.getValueName());
                            return baseAttrValue;
                        }).collect(Collectors.toList());
                this.saveBatch(newBaseAttrValues);
            } else {
                //2、1前端没有传入属性名称,直接删除所有属性值连同属性名称
                //2、2删除属性名称
                baseAttrInfoService.lambdaUpdate()
                        .eq(BaseAttrInfo::getId, vo.getId())
                        .remove();
                //2、3删除所有属性值
                //前端没有传入attrValueList
                this.lambdaUpdate()
                        .eq(BaseAttrValue::getAttrId, vo.getId())
                        .remove();
            }
        } else {
            //3、保存base_attr_info信息
            BaseAttrInfo baseAttrInfo = new BaseAttrInfo();
            BeanUtils.copyProperties(vo, baseAttrInfo);
            baseAttrInfoService.save(baseAttrInfo);
            //4、保存base_attr_value信息
            List<BaseAttrValue> baseAttrValueList = vo.getAttrValueList().stream()
                    .map(item -> {
                        BaseAttrValue baseAttrValue = new BaseAttrValue();
                        baseAttrValue.setAttrId(baseAttrInfo.getId());
                        baseAttrValue.setValueName(item.getValueName());
                        return baseAttrValue;
                    }).collect(Collectors.toList());
            this.saveBatch(baseAttrValueList);
        }

    }
}




